﻿using System;

namespace Automator.Tasks.Core
{
    public class TaskAttributeDefinition
    {
        public string Key { get; set; }
        public object Value { get; set; }
    }
    
    /// <summary>
    /// Class that contains common data for executing workflow
    /// </summary>
    public class WorkflowExecutionContext
    {
        
        #region Fields
        
        private bool _runDependencies = true;
        private int _completed = 0;
        private int _failed = 0;
        
        #endregion
        
        #region Properties

        /// <summary>
        /// Provides input data if needed
        /// </summary>
        public object Argument { get; set; }
        
        /// <summary>
        /// Provides execution result, if needed
        /// </summary>
        public object Result { get; set; }
        
        /// <summary>
        /// Specifies whether Tasks should be executed with dependencies
        /// </summary>
        public bool RunDependencies
        {
            get { return _runDependencies; }
            set { _runDependencies = value; }
        }

        public int CompletedCount
        {
            get { return _completed; }
            set { _completed = value; }
        }

        public int FailedCount
        {
            get { return _failed; }
            set { _failed = value; }
        }
        
        #endregion
        
        #region Methods
        
        /// <summary>
        /// Substitutes variable definitions with their actual values
        /// </summary>
        /// <param name="input">String to format</param>
        /// <returns>Formatted string</returns>
        public string DereferenceVariables(object input)
        {
            string result = input.ToString();
            
            //System variables first
            if (result.Contains("%Result%")) result = (Result == null) ? result.Replace("%Result%", String.Empty) : result.Replace("%Result%", Result.ToString());
            if (result.Contains("%Argument%")) result = (Argument == null) ? result.Replace("%Argument%", String.Empty) : result.Replace("%Argument%", Argument.ToString());
            
            return result.Trim();
        }
        
        #endregion
    }
    
    /// <summary>
    /// Class used for transfer execution data to background thread, if Task execution requires one
    /// </summary>
    public class TaskThreadingContext
    {
        public Task Task { get; private set; }
        public WorkflowExecutionContext ExecutionContext { get; private set; }
        
        public TaskThreadingContext(Task task, WorkflowExecutionContext context)
        {
            this.Task = task;
            this.ExecutionContext = context;
        }
    }
}
